package com.dong.mcd.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dong.mcd.model.SysRole;
import com.dong.mcd.param.RoleParam;
import com.dong.mcd.pub.BaseAction;
import com.dong.mcd.service.IRoleService;
import com.dong.mcd.util.JSONUtil;

@Controller
@RequestMapping("/system/role")
public class RoleAction extends BaseAction{

	@Autowired
	private IRoleService roleService;
	
	/**
	 * 查询菜单
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/query")
	public void queryMenu(HttpServletRequest request, HttpServletResponse response,RoleParam param) throws Exception {
		List<SysRole> menuList= roleService.query(param);
		writeJson(response, menuList);
	}
	
	@RequestMapping("/page")
	public void queryPage(HttpServletRequest request, HttpServletResponse response,RoleParam param) throws Exception {
		
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		writeJson(response, roleService.queryPage(param, pageIndex, pageSize).getPageMap());
	}
	
	@RequestMapping("/modify")
	public void modify(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String menuStr = request.getParameter("jsonData");
		try {
			SysRole role = JSONUtil.toObject(menuStr, SysRole.class);
			if (StringUtils.isEmpty(role.getRoleName())){
				writeError(response, "角色名称不能为空");
				return;
			}
			roleService.modify(role);
			writeSuccess(response, "修改成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/add")
	public void add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String menuStr = request.getParameter("jsonData");
		try {
			SysRole role = JSONUtil.toObject(menuStr, SysRole.class);
			if (StringUtils.isEmpty(role.getRoleName())){
				writeError(response, "角色名称不能为空");
				return;
			}
			roleService.add(role);
			writeSuccess(response, "添加成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/delete")
	public void delete(HttpServletRequest request, HttpServletResponse response,RoleParam param) throws Exception{
		try {
				roleService.remove(param.getId());
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/grantMenu")
	public void grantMenu(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String roleStr = request.getParameter("jsonData");
		try {
				RoleParam role = JSONUtil.toObject(roleStr, RoleParam.class);
				if (role.getId() == null){
					writeError(response, "角色编号不能为空");
					return;
				}
				roleService.grantMenus(role);
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
}
